//
//  ActivityViewController.m
//  testApp
//
//  Created by ColdMountain on 2017/3/14.
//  Copyright © 2017年 ColdMountain. All rights reserved.
//

#import "ActivityViewController.h"

#import "Services.h"

#import <MWPhotoBrowser/MWPhotoBrowser.h>
#import <UINavigationController+FDFullscreenPopGesture.h>

@interface ActivityViewController ()<MWPhotoBrowserDelegate>
@property (weak, nonatomic) IBOutlet UIWebView *ui_webView;
@property (strong, nonatomic) NSDictionary *m_webDic;
@property (strong, nonatomic) NSArray *photos;

@end

@implementation ActivityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ui_webView.scrollView.bounces=NO;
    self.navigationController.fd_prefersNavigationBarHidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getWebViewAction];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)getWebViewAction
{
    [ServiceManager postActivityDetailsWithString:self.ID success:^(NSDictionary *data) {
        self.m_webDic=data;
        [self webViewToString];
    } failure:^(NSDictionary *data) {
        
    }];
}

- (void)webViewToString
{
    [self.ui_webView loadHTMLString:self.m_webDic[@"content"] baseURL:nil];
}

#pragma mark - UIWebViewDelegate

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    //加载完成时 添加H5代码 实现点击图片的点击事件
    [self.ui_webView stringByEvaluatingJavaScriptFromString:@"function assignImageClickAction(){var imgs=document.getElementsByTagName('img');var length=imgs.length;var urls='';for(var i=0;i<length;i++){img=imgs[i];urls=urls+'___YN_XD___'+img.src}for(var i=0;i<length;i++){img=imgs[i];img.onclick=function(){window.location.href='image-preview:'+this.src+'RXD_'+urls}}}"];
    [self.ui_webView stringByEvaluatingJavaScriptFromString:@"assignImageClickAction();"];
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    DLog(@"webViewDidStartLoad");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    DLog(@"webView:(UIWebView *)webView didFailLoadWithError:%@",error);
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //点击图片之后调用
    if ([request.URL.scheme isEqualToString:@"image-preview"])
    {
        NSString* path = [request.URL.absoluteString substringFromIndex:[@"image-preview:" length]];
        path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        NSArray *urls=[path componentsSeparatedByString:@"RXD____YN_XD___"];
        NSString *currentUrl=[urls firstObject];
        NSString *otherUrls=[urls lastObject];
        NSArray *oldUrls=[otherUrls componentsSeparatedByString:@"___YN_XD___"];
        
        NSMutableArray *photos = [NSMutableArray array];
        NSUInteger index=0;
        for (NSUInteger i=0; i<oldUrls.count; i++) {
            NSString *url=[oldUrls objectAtIndex:i];
            if ([url isEqualToString:currentUrl]) {
                index = i;
            }
            [photos addObject:[MWPhoto photoWithURL:[NSURL URLWithString:url]]];
        }
        //调用图片预览方法
        [self didSelectedImageWithIndex:index allImages:photos];
        return NO;
    }
    return YES;
}

- (void)didSelectedImageWithIndex:(NSUInteger)index allImages:(NSArray *)images{
    self.photos=images;
    MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
    [browser setCurrentPhotoIndex:index];
    // Set options
    browser.displayActionButton = YES; // Show action button to allow sharing, copying, etc (defaults to YES)
    browser.displayNavArrows = NO; // Whether to display left and right nav arrows on toolbar (defaults to NO)
    browser.displaySelectionButtons = NO; // Whether selection buttons are shown on each image (defaults to NO)
    browser.zoomPhotosToFill = YES; // Images that almost fill the screen will be initially zoomed to fill (defaults to YES)
    browser.alwaysShowControls = NO; // Allows to control whether the bars and controls are always visible or whether they fade away to show the photo full (defaults to NO)
    browser.enableGrid = YES; // Whether to allow the viewing of all the photo thumbnails on a grid (defaults to YES)
    browser.startOnGrid = NO; // Whether to start on the grid of thumbnails instead of the first photo (defaults to NO)
    browser.autoPlayOnAppear = NO; // Auto-play first video
    [self.navigationController pushViewController:browser animated:YES];
}


#pragma makr - MWPhotoBrowserDelegate

- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
    return self.photos.count;
}

- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
    if (index < self.photos.count) {
        return [self.photos objectAtIndex:index];
    }
    return nil;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
